//
//  ByBonusOptimizeSwitchView.m
//  caipiao_ios
//
//  Created by mc on 2018/9/5.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByBonusOptimizeSwitchView.h"

@implementation ByBonusOptimizeSwitchView

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        NSArray *dataArray = @[@"平均优化",@"搏热优化",@"搏冷优化"];
        CGFloat left = 16;
        CGFloat interval = 16;
        self.index = 0;
        CGFloat width = (SCREEN_WIDTH - left * 2 - interval * 2) / 3.0;
        CGFloat height = 31;
        for (int i = 0; i < dataArray.count; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setTitle:[NSString stringWithFormat:@"%@", dataArray[i]] forState:UIControlStateNormal];
            [btn.titleLabel setFont:[UIFont systemFontOfSize:14]];
            [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            btn.layer.borderWidth = 1;
            btn.layer.borderColor = BY_LINE_COLOR.CGColor;
            btn.frame = CGRectMake(left + i * (interval + width) + i , 0, width, height);
            [self addSubview:btn];
            [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag = 100 + i;
            if (i == 0) {
               btn.layer.borderColor = BY_THEME_COLOR.CGColor;
               [btn setTitleColor:BY_THEME_COLOR forState:UIControlStateNormal];
            }
        }
    }
    return self;
}


- (void)btnAction:(UIButton *)btn{
//    if (btn.tag == self.index + 100) {
//        return;
//    }else{
        UIButton *btn1 = [self viewWithTag:self.index + 100];
        btn1.layer.borderColor = BY_LINE_COLOR.CGColor;
        [btn1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        self.index = btn.tag - 100;
        btn.layer.borderColor = BY_THEME_COLOR.CGColor;
        [btn setTitleColor:BY_THEME_COLOR forState:UIControlStateNormal];
        
        if (self.selectValueBlock) {
            self.selectValueBlock(self.index);
        }
    //}
}


@end
